package org.jeecg.modules.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.modules.common.DateCommon;
import org.jeecg.modules.entity.DbContract;
import org.jeecg.modules.entity.DbEmployeeData;
import org.jeecg.modules.entity.DbFile;
import org.jeecg.modules.entity.DbSeq;
import org.jeecg.modules.mapper.DbEmployeeDataMapper;
import org.jeecg.modules.service.IDbEmployeeDataService;
import org.jeecg.modules.service.IDbFileService;
import org.jeecg.modules.service.IDbSeqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: db_employee_data
 * @Author: jeecg-boot
 * @Date:   2024-07-08
 * @Version: V1.0
 */
@Service
public class DbEmployeeDataServiceImpl extends ServiceImpl<DbEmployeeDataMapper, DbEmployeeData> implements IDbEmployeeDataService {

    @Autowired
    private IDbFileService dbFileService;
    @Autowired
    private IDbSeqService dbSeqService;
    /**
     *   添加
     *
     * @param dbEmployeeData
     * @return
     */
    @Transactional
    public void add(DbEmployeeData dbEmployeeData) {
        //编号生成
        DbSeq dbSeq = new DbSeq();
        QueryWrapper<DbSeq> queryWrapper = new QueryWrapper<>();
        Date currentDate = new Date();
        String formattedDate = DateCommon.YYYYMMDD.format(currentDate);
        String name = "db_employee"+formattedDate;
        queryWrapper.eq("name",name);
        DbSeq dbSeq1 = dbSeqService.getOne(queryWrapper);
        if(dbSeq1 == null){
            dbSeq.setName(name);
            dbSeq.setSeq(0001);
            String id=formattedDate+"0001";
            dbSeqService.save(dbSeq);
            dbEmployeeData.setId(id);
            this.save(dbEmployeeData);
        }else{
            Integer seq = dbSeq1.getSeq();
            seq+=1;
            UpdateWrapper<DbSeq> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("seq",seq).eq("name",dbSeq1.getName());
            dbSeqService.update(dbSeq1,updateWrapper);
            // 使用String.format()来确保序列号是4位数，不足的前面补0
            String seqStr = String.format("%04d", seq);
            String id=formattedDate+seqStr;
            dbEmployeeData.setId(id);
            this.save(dbEmployeeData);
        }
    }
    //插入文件组id file_group_id
    private void updateFileGroupId(DbEmployeeData dbEmployeeData){
        if(dbEmployeeData.getFileIds()==null||dbEmployeeData.getFileIds().isEmpty()){
            this.save(dbEmployeeData);
        }else {
            dbEmployeeData.setFileGroupId(null);
            this.save(dbEmployeeData);
            UpdateWrapper<DbEmployeeData> dbEmployeeDataUpdateWrapper = new UpdateWrapper<>();
            String fileGroupId = "db_employee"+dbEmployeeData.getId();
            dbEmployeeDataUpdateWrapper.set("file_group_id",fileGroupId).eq("id",dbEmployeeData.getId());
            this.update(dbEmployeeData,dbEmployeeDataUpdateWrapper);
            UpdateWrapper<DbFile> updateWrapper = new UpdateWrapper<>();
            DbFile dbFile = new DbFile();
            updateWrapper.set("group_id", fileGroupId).in("id",dbEmployeeData.getFileIds());
            dbFileService.update(dbFile,updateWrapper);
        }
    }
}
